package com.example.taobao

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import kotlinx.android.synthetic.main.fragment_home_page.*
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.Exception
import java.net.HttpURLConnection
import java.net.URL
import kotlin.concurrent.thread

class HomePage : Fragment() {

    var shopList = ArrayList<shop>()

    var layoutManager : GridLayoutManager? = null
    var adapter : shopAdapter? = null

    val handle = @SuppressLint("HandlerLeak")
    object : Handler()
    {
        override fun handleMessage(msg: Message) {
            shopList = msg.obj as ArrayList<shop>
            layoutManager = GridLayoutManager(context, 2)
            card1.layoutManager = layoutManager
            adapter = context?.let { shopAdapter(it, shopList) }
            card1.adapter = adapter
            adapter?.setOnItemClickListener(object: shopAdapter.OnItemClickListener {
                override fun onItemClick(view: View, position: Int) {
                    val ins = Intent(context, MainDetails::class.java)
                    ins.putExtra("id", shopList[position].id)
                    startActivity(ins)
                }

            })
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_home_page,container,false)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

        super.onViewCreated(view, savedInstanceState)

        initSearch()
        /*    val layoutManager = GridLayoutManager(this, 2)
           card1.layoutManager = layoutManager
           val adapter = shopAdapter(this, shopList)
            card1.adapter = adapter*/



        sousuo.setOnClickListener {
            val intent = Intent(context, MainActivity2::class.java)
            startActivity(intent)

        }
        intent_tosort.setOnClickListener {
            val intent = Intent(context, sort::class.java)
            startActivity(intent)
        }



    }

    private fun initSearch()
    {
        val us = "http://192.168.43.140:8080/aaa_war_exploded/random.jsp"
        thread {
            var url = URL(us)
            //var url = URL("https://www.baidu.com")
            val connection = url.openConnection() as HttpURLConnection
            try {
                val response = StringBuilder()
                connection.connectTimeout = 8000
                connection.readTimeout = 8000
                connection.requestMethod = "POST"

                connection.setRequestProperty("Accept-Charset", "UTF-8" )
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
                connection.connectTimeout = 8000
                connection.readTimeout = 8000
                val input = connection.inputStream
                val reader = BufferedReader(InputStreamReader(input) )

                var src : String = ""
                var id : Int = 0
                var names:String = ""
                var prices:String = ""
                var i = 0

                var meg : ArrayList<shop> = ArrayList<shop>()

                reader.use {
                    reader.forEachLine {
                        if(it.length > 0) {
                            if( i == 0)
                                id = it.toInt()
                            else if( i == 1)
                                src = it
                            else if ( i == 2)
                                names = it
                            else if( i == 3)
                            {
                                i = -1
                                meg.add(shop(id, names, src,  prices ) )
                            }
                            i++
                        }
                    }
                }

                shopList = meg
                val msg = Message()
                msg.obj = meg
                handle.sendMessage(msg)


                //  showResponse(response.toString() )
            } catch (e: Exception )
            {
                e.printStackTrace();
            } finally {
                connection.disconnect()
            }
        }

    }

}